package org.makcro.sms.services.chat.web.client.widgets.bulk;

import org.makcro.sms.services.chat.web.client.AppEvents.BulkEvents;
import org.makcro.sms.services.chat.web.client.Constants;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;

public class SendBulkMessageDialog extends Dialog {
	private TextField<String>	receiverTextField;
	private TextArea			txtrNewTextarea;
	private Button				btnSend;
	private Button				btnCancel;
	private Status				charCount;
	private ToolBar				statusBar;

	public SendBulkMessageDialog() {
		setModal( true );
		setHeading( "Slanje bulk poruke" );
		setBodyStyle( "padding: 8px;background: none" );
		setButtons( "" );
		setWidth( 600 );
		setHeight( 400 );
		setLayout( new FormLayout() );
		FormData fd_nmbrfldReceiver = new FormData( "95%" );
		fd_nmbrfldReceiver.setMargins( new Margins( 10, 0, 0, 0 ) );
		add( getReceiverTextField(), fd_nmbrfldReceiver );
		add( getTxtrNewTextarea(), new FormData( "95% -90" ) );
		getButtonBar().add( getBtnSend() );
		getButtonBar().add( getBtnCancel() );
		setBottomComponent( getStatusBar() );
	}

	public TextField<String> getReceiverTextField() {
		if (receiverTextField == null) {
			receiverTextField = new TextField<String>();
			receiverTextField.setRegex( "^381(60|61|62|63|64|65|66|69).{6,}" );
			receiverTextField.getMessages().setRegexText( "Broj telefona nije u ispravnom formatu" );
			receiverTextField.setAllowBlank( false );
			receiverTextField.setAutoValidate( true );
			receiverTextField.setFieldLabel( "Primalac" );
		}
		return receiverTextField;
	}

	public TextArea getTxtrNewTextarea() {
		if (txtrNewTextarea == null) {
			txtrNewTextarea = new TextArea();
			txtrNewTextarea.setFieldLabel( "Text poruke" );
			txtrNewTextarea.addListener( Events.OnKeyUp, new Listener<FieldEvent>() {

				@Override
				public void handleEvent(FieldEvent be) {
					onMessageTextChanged( be );
				}
			} );
			// set validation
			txtrNewTextarea.setMaxLength( 160 );
			txtrNewTextarea.getMessages().setMaxLengthText( "Poruka ne sme biti duza od 160 karaktera" );
			txtrNewTextarea.setAutoValidate( true );
			txtrNewTextarea.setAllowBlank( false );
		}
		return txtrNewTextarea;
	}

	/**
	 * Handler for text area text changed event. Updates character count
	 * 
	 * @param be
	 */
	protected void onMessageTextChanged(FieldEvent be) {
		// extract field from event
		TextArea bulkMesageTextArea = (TextArea) be.getField();
		String bulkMessageText = bulkMesageTextArea.getValue();
		// calculate message length
		int length = bulkMessageText != null ? bulkMessageText.length() : 0;
		// update status
		getCharCount().setText( length + (length == 1 ? " karakter" : " karaktera") );
	}

	private Button getBtnSend() {
		if (btnSend == null) {
			btnSend = new Button( "Posalji" );
			btnSend.addListener( Events.Select, new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
					onBtnSendSelected( ce );
				}
			} );
		}
		return btnSend;
	}

	protected void onBtnSendSelected(ButtonEvent ce) {

		// validate form
		boolean receiverValid = getReceiverTextField().isValid();
		boolean textValid = getTxtrNewTextarea().isValid();

		if (receiverValid && textValid) {
			AppEvent appEvent = new AppEvent( BulkEvents.BULK_MESSAGE_ABOUT_TO_BE_SENT );

			appEvent.setData( Constants.EventData.BULK_RECEIVER, getReceiverTextField().getValue() );
			appEvent.setData( Constants.EventData.BULK_MESSAGE_TEXT, getTxtrNewTextarea().getValue() );
			getButtonBar().disable();

			Dispatcher.get().dispatch( appEvent );
			
		}
		else {
			Info.display( "Greska", "Proverite podatke" );
		}
	}

	private Button getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new Button( "Otkazi" );
			btnCancel.addListener( Events.Select, new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
					onBtnCancelSelected();
				}
			} );
		}
		return btnCancel;
	}

	protected void onBtnCancelSelected() {
		resetForm();
		this.hide();

	}

	public void resetForm() {
		// reset all fields
		getReceiverTextField().setValue( "" );
		getTxtrNewTextarea().setValue( "" );
		getCharCount().setText( "0 karaktera" );
	}

	public Status getCharCount() {
		if (this.charCount == null) {
			this.charCount = new Status();
			this.charCount.setText( "0 karaktera" );
			this.charCount.setBox( true );
		}
		return charCount;
	}

	public ToolBar getStatusBar() {
		if (this.statusBar == null) {
			this.statusBar = new ToolBar();
			this.statusBar.add( getCharCount() );
		}
		return statusBar;
	}
}
